package com.ss.android.video.renderview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.ViewGroup;
import com.ss.android.article.base.feature.video.SSSurfaceView;
import com.ss.android.video.SSMediaPlayerWrapper;
import com.ss.android.video.WeakSurfaceHolderCallbackWrapper;
import java.util.ArrayList;
import java.util.Iterator;

public class SSRenderSurfaceView extends SSSurfaceView implements SurfaceHolder.Callback, IRenderView {
    private IRenderCallback mRenderCallback;
    private WeakSurfaceHolderCallbackWrapper mWeakSurfaceHolderCallbackWrapper;
    private static ArrayList c = new ArrayList();
    private WindowVisibilityChangedListener mWindowVisibilityChangedListener;

    public SSRenderSurfaceView(Context context) {
        super(context);
        this.init();
    }

    public SSRenderSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init();
    }

    public SSRenderSurfaceView(Context context, AttributeSet attrs, int defaultStyle) {
        super(context, attrs, defaultStyle);
        this.init();
    }

    private void init() {
        this.mWeakSurfaceHolderCallbackWrapper = new WeakSurfaceHolderCallbackWrapper(((SurfaceHolder.Callback)this));
        SSRenderSurfaceView.c.add(this.mWeakSurfaceHolderCallbackWrapper);
    }

    public final void resize(int width, int height) {
        ViewGroup.LayoutParams layoutParams = this.getLayoutParams();
        layoutParams.height = height;
        layoutParams.width = width;
        this.setLayoutParams(layoutParams);
    }

    public final void setRenderCallback(IRenderCallback renderCallback) {
        this.mRenderCallback = renderCallback;
        SurfaceHolder holder = this.getHolder();
        holder.setFormat(-3);
        if(SSMediaPlayerWrapper.mediaPlayerType == 0) {
            holder.setType(3);
        }

        Iterator<WeakSurfaceHolderCallbackWrapper> weakCallbackWrapperIterator = SSRenderSurfaceView.c.iterator();
        while(weakCallbackWrapperIterator.hasNext()) {
            WeakSurfaceHolderCallbackWrapper callbackWrapper = weakCallbackWrapperIterator.next();
            if(callbackWrapper.refCallback.get() != null) {
                continue;
            }
            holder.removeCallback(callbackWrapper);
            weakCallbackWrapperIterator.remove();
        }
        holder.addCallback(this.mWeakSurfaceHolderCallbackWrapper);
    }

    public View getView() {
        return this;
    }

    public void onWindowVisibilityChanged(int visibility) {
        super.onWindowVisibilityChanged(visibility);
        if(this.mWindowVisibilityChangedListener != null) {
            this.mWindowVisibilityChangedListener.onWindowVisibilityChanged(visibility);
        }
    }

    public void setWindowVisibilityChangedListener(WindowVisibilityChangedListener windowVisibilityChangedListener) {
        this.mWindowVisibilityChangedListener = windowVisibilityChangedListener;
    }

    public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
        if(this.mRenderCallback != null) {
            this.mRenderCallback.surfaceChanged(surfaceHolder, format, width, height);
        }
    }

    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        if(this.mRenderCallback != null) {
            this.mRenderCallback.surfaceCreated(surfaceHolder);
        }
    }

    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        if(this.mRenderCallback != null) {
            this.mRenderCallback.surfaceDestroyed(surfaceHolder);
        }
    }
}

